import UIKit
import SnapKit

class PlaySettingOptionSelector<T: Equatable>: UIView {
    // MARK: - Properties
    private let titleLabel = UILabel()
    private let optionsStackView = UIStackView()
    
    private var options: [T] = []
    private var unit: String = ""
    private var selectedOption: T?
    private var selectedIndex: Int = 0
    private var optionButtons: [UIButton] = []
    
    // MARK: - Callbacks
    var onSelectedIndexChanged: ((Int) -> Void)?
    var onSelectedOptionChanged: ((T) -> Void)?
    
    // MARK: - Initialization
    init(title: String, options: [T], unit: String, selectedOption: T) {
        self.options = options
        self.unit = unit
        self.selectedOption = selectedOption
        
        if let index = options.firstIndex(where: { $0 as AnyObject === selectedOption as AnyObject || $0 == selectedOption }) {
            self.selectedIndex = index
        }
        
        super.init(frame: .zero)
        
        setupUI()
        configureTitle(title)
        createOptionButtons()
        updateButtonStates()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI Setup
    private func setupUI() {
        titleLabel.textColor = UIColor(hexString: "#000000")
        titleLabel.font = .systemFont(ofSize: 15, weight: .medium)
        titleLabel.textAlignment = .center
        
        optionsStackView.axis = .horizontal
        optionsStackView.alignment = .center
        optionsStackView.distribution = .equalSpacing
        optionsStackView.spacing = 23
        
        addSubview(titleLabel)
        addSubview(optionsStackView)
        
        titleLabel.snp.makeConstraints { make in
            make.top.leading.equalToSuperview()
        }
        
        optionsStackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(13)
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    private func configureTitle(_ title: String) {
        titleLabel.text = title
    }
    
    private func createOptionButtons() {
        optionButtons.forEach { $0.removeFromSuperview() }
        optionButtons.removeAll()
        
        options.enumerated().forEach { index, option in
            let button = UIButton(type: .system)
            button.setTitle("\(option)\(unit)", for: .normal)
            button.setTitleColor(UIColor(hexString: "#C9CEDA"), for: .normal)
            button.titleLabel?.font = .systemFont(ofSize: 15, weight: .medium)
            button.layer.borderWidth = 0.5
            button.layer.borderColor = UIColor(hexString: "#C9CEDA")?.cgColor
            button.layer.cornerRadius = 12.5
            button.tag = index
            button.addTarget(self, action: #selector(optionButtonTapped(_:)), for: .touchUpInside)
            
            optionsStackView.addArrangedSubview(button)
            optionButtons.append(button)

            button.snp.makeConstraints { make in
                make.height.equalTo(25)
                make.width.equalTo(50)
            }
        }
    }
    
    // MARK: - Button Actions
    @objc private func optionButtonTapped(_ sender: UIButton) {
        selectedIndex = sender.tag
        updateButtonStates()
        onSelectedIndexChanged?(selectedIndex)
        onSelectedOptionChanged?(options[selectedIndex])
    }
    
    // MARK: - Public Methods
    func setSelectedIndex(_ index: Int) {
        guard index >= 0 && index < options.count else { return }
        selectedIndex = index
        updateButtonStates()
    }
    
    func getSelectedOption() -> T? {
        guard selectedIndex >= 0 && selectedIndex < options.count else { return nil }
        return options[selectedIndex]
    }
    
    // MARK: - Helper Methods
    private func updateButtonStates() {
        optionButtons.enumerated().forEach { index, button in
            if index == selectedIndex {
                button.setTitleColor(UIColor(hexString: "#15CC9D"), for: .normal)
                button.layer.borderColor = UIColor(hexString: "#15CC9D")?.cgColor
            } else {
                button.setTitleColor(UIColor(hexString: "#C9CEDA"), for: .normal)
                button.layer.borderColor = UIColor(hexString: "#C9CEDA")?.cgColor
            }
        }
    }
}

class PlaySettingSwitchOptionSelector: UIView {
    var onSwitchChanged: ((Bool) -> Void)?
    
    private let switchControl = UISwitch()
    
    init(title: String, switchOn: Bool, onSwitchChangedAction: ((Bool) -> Void)?) {
        super.init(frame: .zero)
        setupUI(title: title, switchOn: switchOn)
        onSwitchChanged = onSwitchChangedAction
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI Setup
    private func setupUI(title: String, switchOn: Bool) {
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = UIColor(hexString: "#000000")
        titleLabel.font = .systemFont(ofSize: 15, weight: .medium)
        titleLabel.textAlignment = .center

        switchControl.isOn = switchOn

        let stackView = UIStackView(arrangedSubviews: [titleLabel, switchControl])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.spacing = 9
        
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalToSuperview()
        }
    }
    
    // MARK: - Button Actions
    @objc private func switchButtonTapped() {
        switchControl.isSelected.toggle()
        onSwitchChanged?(switchControl.isSelected)
    }
}
